//
//  SPMusicFormatDelegate.m
//  Sophie
//
//  Created by Anton Chebotaev on 19/11/09.
//  Copyright 2009 IFMO. All rights reserved.
//

#import "SPMusicFormatDelegate.h"


@implementation SPMusicFormatDelegate

#pragma mark NSTextFieldDelegate
- (BOOL)tokenField:(NSTokenField *)tokenField hasMenuForRepresentedObject:(id)representedObject {
	return NO;
}

- (NSString *)tokenField:(NSTokenField *)tokenField editingStringForRepresentedObject:(id)representedObject {
	if ([representedObject isKindOfClass:[SPMusicFormatToken class]]) {
		SPMusicFormatToken *token = (SPMusicFormatToken*)representedObject;
		return [token content];
	} else {
		return [representedObject description];
	}

}
- (NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject {
	if ([representedObject isKindOfClass:[SPMusicFormatToken class]]) {
		SPMusicFormatToken *token = (SPMusicFormatToken*)representedObject;
		return [token content];
	} else {
		return [representedObject description];
	}
}

- (NSArray *)tokenField:(NSTokenField *)tokenField
completionsForSubstring:(NSString *)substring
		   indexOfToken:(NSInteger)tokenIndex
	indexOfSelectedItem:(NSInteger *)selectedIndex
{
	return [NSArray array];		
}

- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString
{	
	return [SPMusicFormatToken tokenWithString:editingString];
}

- (NSTokenStyle)tokenField:(NSTokenField *)tokenField styleForRepresentedObject:(id)representedObject
{
	if ([representedObject isKindOfClass:[SPMusicFormatToken class]]) {
		SPMusicFormatToken *token = (SPMusicFormatToken*)representedObject;
		return [token style];
	} else {
		return NSPlainTextTokenStyle;
	}

}

- (BOOL)tokenField:(NSTokenField *)tokenField writeRepresentedObjects:(NSArray *)objects toPasteboard:(NSPasteboard *)pboard
{
	// Declare types
    [pboard declareTypes:[NSArray arrayWithObject:[SPMusicFormatToken description]]
				   owner:self];
	
    // Copy data to the pasteboard
    [pboard setData:[NSKeyedArchiver archivedDataWithRootObject:objects] forType:[SPMusicFormatToken description]];
	
	return YES;
}

- (NSArray *)tokenField:(NSTokenField *)tokenField readFromPasteboard:(NSPasteboard *)pboard
{
	// Is there a string on the pasteboard?
    NSArray *types = [pboard types];
	if ([types containsObject:[SPMusicFormatToken description]]) {
		
        // Read the data from the pasteboard
		NSData *data = [pboard dataForType:[SPMusicFormatToken description]];
        NSArray *result = [NSKeyedUnarchiver unarchiveObjectWithData:data];
		
		return result;
    }
	else if ([types containsObject:NSStringPboardType]) {
		
        // Read the string from the pasteboard
		NSString *string = [pboard stringForType:NSStringPboardType];
        //NSArray *result = [NSArray arrayWithObject:[SPMusicFormatToken tokenWithString:string]]; //TODO: Review
        NSArray *result = [NSArray arrayWithObject:string];
		
		return result;
    } else {
		return [NSArray array];
	}

}

@end
